package com.zhongwang.mms.module.half.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.module.half.dao.HOrderDao;
import com.zhongwang.mms.module.half.model.HOrderDetailModel;
import com.zhongwang.mms.module.half.service.HBasicService;
import com.zhongwang.mms.module.half.service.InAndOutService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 出入库查询页面
 */
@Controller
@RequestMapping("/half/halWarManage/inAndOutQuery")
public class InAndOutController  extends BaseController {

    private String prefix = "half/halWarManage/inAndOutQuery";
    @Autowired
    private InAndOutService inAndOutService;
    @Autowired
    private HBasicService HBasicService;
    @Autowired
    private HOrderDao hOrderDao;

    @GetMapping("/page")
    @RequiresPermissions({"halwar:halwarquery:view"})
    public String page(Model model) {

        model.addAttribute("warInfo", HBasicService.getFinishWarInfo());
        return prefix + "/list";
    }

    /**
     * 出入库查询
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"halwar:halwarquery:view"})
    public JsonResult list(HOrderDetailModel hOrderDetailModel) {
        startPage();
        List<HOrderDetailModel> list = inAndOutService.findAll(hOrderDetailModel);
        return jsonResult(list);
    }

    @GetMapping("/view")
    @RequiresPermissions(value = {"halwar:halmakeinwar:view", "halwar:halpurinwar:view", "halwar:halallinwar:view", "halwar:halmakeoutwar:view", "halwar:halpuroutwar:view"}, logical = Logical.OR)
    public String view(@RequestParam(value = "odrId", required = false) Integer odrId, @RequestParam(value = "odrOutType", required = false) Integer odrOutType, @RequestParam(value = "odaMatType", required = false) Integer odaMatType, Model model) {

        String prefix = "";
        if ((odrOutType == 2 || odrOutType == 8 ) && odaMatType == 3) {
            // 制作件入库
            prefix = "half/halWarManage/inAndOutQuery/finishWarPage";
        } else if ((odrOutType == 1 || odrOutType == 2) && (odaMatType == 1 || odaMatType == 2)) {
            // 外购件入库
            prefix = "half/halWarManage/inAndOutQuery/inBuyWarPage";
        } else if (odrOutType == 3) {
            // 调拨入库
            prefix = "half/halWarManage/inAndOutQuery/allocationInWarPage";
        } else if ((odrOutType == 4 ||odrOutType == 7) && odaMatType == 3) {
            // 制作件出库
            prefix = "half/halWarManage/inAndOutQuery/outOfFinishWarPage";
        } else if ((odrOutType == 4 ||odrOutType == 7) && (odaMatType == 1 || odaMatType == 2)) {
            // 外购件出库
            prefix = "half/halWarManage/inAndOutQuery/outOfBuyWarPage";
        } else if((odrOutType == 4 || odrOutType == 7) && odaMatType == 4){
            //铝料出库
            prefix = "half/halWarManage/inAndOutQuery/outOfAluminWarPage";
        } else if((odrOutType == 1 || odrOutType == 2) && odaMatType == 4){
            //铝料入库
            prefix = "half/halWarManage/inAndOutQuery/inAluminWarPage";
        }
        model.addAttribute("warInfo", HBasicService.getWarInfo());
        model.addAttribute("dept", HBasicService.getDept());
        model.addAttribute("order", hOrderDao.findAllById(odrId));
        return prefix;
    }

    /**
     * 查询入库信息
     * @param hOrderDetailModel
     * @return
     */
    @ResponseBody
    @GetMapping("/getInFinish")
    @RequiresPermissions({"halwar:halwarquery:view"})
    public JsonResult getInFinish(HOrderDetailModel hOrderDetailModel) {


        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(inAndOutService.getInFinish(hOrderDetailModel));
        jsonResult.setType(JsonResult.ResultType.SUCCESS);
        return jsonResult;
    }

    /**
     * 导出
     * @param response
     * @param param
     */
    @ResponseBody
    @PostMapping("/exportExcel")
    @RequiresPermissions({"halwar:halwarquery:export"})
    public JsonResult exportExcel(HOrderDetailModel param) {
       return inAndOutService.exportExcel(param);
    }

    /**
     * 调拨入库查询
     */
    @ResponseBody
    @PostMapping("/allocationList")
    //@RequiresPermissions({"halwar:halwarquery:view"})
    public JsonResult allocationList(HOrderDetailModel param) {
        startPage();
        List<HOrderDetailModel> list = inAndOutService.selectAllocationDetailList(param);
        return jsonResult(list);
    }
}
